/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.googlecode.milib.tools {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * <p>Lightweight class created withoud using any MILib classes
	 * for loading main swf's (swf with AS, swf with graphic), and data.</p>
	 * 
	 * @author Marek Brun
	 */
	public class LoadingsObserver extends EventDispatcher {
		
		//Events
		public static var LoadingsObserverInit : String = 'LoadingsObserverInit';
		public static var LoadingsObserverBytesTotalAchieved : String = 'LoadingsObserverBytesTotalAchieved';
		public static var LoadingsObserverProgress : String = 'LoadingsObserverProgress';
		public static var LoadingsObserverEnd : String = 'LoadingsObserverEnd';
		
		private var loadObjs:Array;
		private var phaseNow : Object;
		private var phase_1_GetObjectsBytesTotal : Object = {name:'GetObjectsBytesTotal'};
		private var phase_2_GetObjectsBytesLoaded : Object = {name:'GetObjectsBytesLoaded'};
		private var phase_3_End : Object = {name:'End'};
		private var objectsWithNoTotalBytes:Array;
		private var bytesTotal:Number;
		private var lastBytesLoaded:Number;	
		private var timer:Timer;		
		private var loadInfosCount:Number;
		private var countFosho:int;
		
		public function LoadingsObserver(loadObjs:Array) {
			this.loadObjs=loadObjs;
			
//			loadInfos=loadObjs.filter(function(item:*, index:int, array:Array):Boolean {
//				return item instanceof LoaderInfo;
//			});
			
			loadInfosCount=loadObjs.length;
			for(var i:uint=0;i<loadObjs.length;i++){
				loadObjs[i].addEventListener(Event.COMPLETE, onLoaderInfoComplete, false, 0, true);
			}
			
		}
		
		public function start():void {
			countFosho=4;
			phaseNow=phase_1_GetObjectsBytesTotal;
			
			//intervalID=setInterval(onInterval, int(1000/40));
			timer=new Timer(int(1000/40), 0);
			timer.addEventListener('timer', onInterval, false, 0, true);
			timer.start();
			
			objectsWithNoTotalBytes=loadObjs.concat();
			dispatchEvent(new Event(LoadingsObserverInit));
			phaseNow=phase_1_GetObjectsBytesTotal;
		}
		
		public function getProgress():Number{
			switch(phaseNow){
				case phase_2_GetObjectsBytesLoaded:
					return lastBytesLoaded/bytesTotal;
				break;
				case phase_3_End:
					return 1;
				break;
				default:
					return 0;
			}
		}
		
//********************************************************************************************
//		events for LoadingsObserver
//********************************************************************************************
		private function onInterval(event:TimerEvent):void {
			var bytesLoaded:Number,i:uint,obj:*;
			switch(phaseNow){
				case phase_1_GetObjectsBytesTotal:
					for(i=0;i<objectsWithNoTotalBytes.length;i++){
						if(objectsWithNoTotalBytes[i].bytesTotal>0){
							objectsWithNoTotalBytes.splice(i, 1);
							i--;
						}
					}
					
					if(!objectsWithNoTotalBytes.length){
						bytesTotal=0;
						for(i=0;i<loadObjs.length;i++){
							bytesTotal+=loadObjs[i].bytesTotal;
						}
						bytesLoaded=0;
						for(i=0;obj=loadObjs[i];i++){
							bytesLoaded+=obj.bytesLoaded;
						}
						lastBytesLoaded=bytesLoaded;
						phaseNow=phase_2_GetObjectsBytesLoaded;
						dispatchEvent(new Event(LoadingsObserverBytesTotalAchieved));
					}
				break;
				case phase_2_GetObjectsBytesLoaded:
					bytesLoaded=0;
					for(i=0;obj=loadObjs[i];i++){
						bytesLoaded+=obj.bytesLoaded;
					}
					if(lastBytesLoaded!=bytesLoaded){
						lastBytesLoaded=bytesLoaded;
						dispatchEvent(new Event(LoadingsObserverProgress));
					}
					if(bytesLoaded>=bytesTotal || loadInfosCount==0){
						countFosho--;
						if(countFosho<1){
							phaseNow=phase_3_End;
						}
					}
				break;
				case phase_3_End:
					timer.stop();
					dispatchEvent(new Event(LoadingsObserverEnd));
				break;
			}
		}
		
		private function onLoaderInfoComplete(event:Event):void {
			loadInfosCount--;
			if(loadInfosCount==0){
				phaseNow=phase_3_End;
			}
		}
		
	}
}
